﻿#region 命名空间

using System;

#endregion

namespace Sharp.Infrastructure
{
    /// <summary>
    ///     日期助手类
    /// </summary>
    public class MyDateTime
    {
        #region 比较日期的年份差

        /// <summary>
        ///     比较日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <returns>与当前日期的年份差</returns>
        public static int DiffYear(string start)
        {
            return DiffYear(Convert.ToDateTime(start));
        }

        /// <summary>
        ///     比较日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <returns>年份差</returns>
        public static int DiffYear(string start, string end)
        {
            return DiffYear(Convert.ToDateTime(start), Convert.ToDateTime(end));
        }

        /// <summary>
        ///     比较日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <returns>与当前日期的年份差</returns>
        public static int DiffYear(DateTime start)
        {
            return (DiffYear(start, DateTime.Now));
        }

        /// <summary>
        ///     比较两个日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <returns>年份差</returns>
        public static int DiffYear(DateTime start, DateTime end)
        {
            return (end.Year - start.Year);
        }

        #endregion

        #region 格式化当天日期

        /// <summary>
        ///     格式化当天日期(yyyy-MM-dd)
        /// </summary>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat()
        {
            return DateFormat(DateTime.Now);
        }

        /// <summary>
        ///     格式化日期(yyyy-MM-dd)
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(string date)
        {
            return DateFormat(Convert.ToDateTime(date));
        }

        /// <summary>
        ///     格式化日期
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <param name="format">格式化串</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(string date, string format)
        {
            return DateFormat(Convert.ToDateTime(date), format);
        }

        /// <summary>
        ///     格式化日期(yyyy-MM-dd)
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(DateTime date)
        {
            return DateFormat(date, "yyyy-MM-dd");
        }

        /// <summary>
        ///     格式化日期
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <param name="format">格式化串</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(DateTime date, string format)
        {
            return date.ToString(format);
        }

        /// <summary>
        ///     格式化日期时间
        /// </summary>
        /// <param name="datetime">待格式化的日期时间</param>
        /// <returns>格式化后的日期时间字符串</returns>
        public static string DateTimeFormat(DateTime datetime)
        {
            return DateTimeFormat(datetime, "yyyy-MM-dd HH:mm");
        }

        /// <summary>
        ///     格式化日期时间
        /// </summary>
        /// <param name="datetime">待格式化的日期时间</param>
        /// <param name="format">格式化串</param>
        /// <returns>格式化后的日期时间字符串</returns>
        public static string DateTimeFormat(DateTime datetime, string format)
        {
            return datetime.ToString(format);
        }

        #endregion

        #region 年月日差

        /// <summary>
        ///     年月日差
        /// </summary>
        /// <param name="interval">The interval.</param>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <returns>System.Double.</returns>
        public static double DateDiff(DateDiffOption interval, DateTime? startDate, DateTime? endDate)
        {
            if (startDate == null)
            {
                return double.MinValue;
            }
            if (endDate == null)
            {
                return double.MaxValue;
            }
            double lngDateDiffValue = 0;
            DateTime _EndDate = (DateTime) endDate;
            DateTime _StartDate = (DateTime) startDate;
            TimeSpan ts = new TimeSpan(_EndDate.Ticks - _StartDate.Ticks);
            switch (interval)
            {
                case DateDiffOption.Milliseconds:
                    lngDateDiffValue = ts.TotalMilliseconds;
                    break;
                case DateDiffOption.Second:
                    lngDateDiffValue = ts.TotalSeconds;
                    break;
                case DateDiffOption.Minute:
                    lngDateDiffValue = ts.TotalMinutes;
                    break;
                case DateDiffOption.Hour:
                    lngDateDiffValue = ts.TotalHours;
                    break;
                case DateDiffOption.Day:
                    lngDateDiffValue = ts.Days;
                    break;
                case DateDiffOption.Week:
                    lngDateDiffValue = (ts.Days/7);
                    break;
                case DateDiffOption.Month:
                    lngDateDiffValue = (ts.Days/30);
                    break;
                case DateDiffOption.Quarter:
                    lngDateDiffValue = ((ts.Days/30)/3);
                    break;
                case DateDiffOption.Year:
                    lngDateDiffValue = (ts.Days/365);
                    break;
            }
            return (lngDateDiffValue);
        }

        #endregion

        /// <summary>
        ///     输入的二个时间之差，精度：分
        /// </summary>
        /// <param name="small">The small.</param>
        /// <returns>System.Double.</returns>
        public static double GetTimeSpan(DateTime small)
        {
            return GetTimeSpan(DateTime.Now, small);
        }

        /// <summary>
        ///     输入的二个时间之差，精度：分
        /// </summary>
        /// <param name="big">The big.</param>
        /// <param name="small">The small.</param>
        /// <returns>System.Double.</returns>
        public static double GetTimeSpan(DateTime big, DateTime small)
        {
            TimeSpan ts = big - small;
            return ts.TotalMinutes;
        }

        /// <summary>
        ///     获得星期几
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <returns>System.Int32.</returns>
        public static int GetWeekNum(DateTime dt)
        {
            DateTime dt1 = new DateTime(dt.Year, 1, 1);
            TimeSpan ts = dt - dt1;
            return (ts.Days + (int) dt1.DayOfWeek)/7 + 1;
        }

        /// <summary>
        /// 时间改为几个月,几天前,几小时前,几分钟前,或几秒前
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string DateStringFromNow(DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.TotalDays > 60)
            {
                return dt.ToShortDateString();
            }
            else
            if (span.TotalDays > 30)
            {
                return "1个月前";
            }
            else
            if (span.TotalDays > 14)
            {
                return "2周前";
            }
            else
            if (span.TotalDays > 7)
            {
                return "1周前";
            }
            else
            if (span.TotalDays > 1)
            {
                return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
            }
            else
            if (span.TotalHours > 1)
            {
                return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
            }
            else
            if (span.TotalMinutes > 1)
            {
                return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
            }
            else
            if (span.TotalSeconds >= 1)
            {
                return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
            }
            else
            {
                return "1秒前";
            }
        }
    }

    /// <summary>
    ///     日期相差计算枚举
    /// </summary>
    public enum DateDiffOption
    {
        /// <summary>
        ///     微秒
        /// </summary>
        Milliseconds,

        /// <summary>
        ///     秒
        /// </summary>
        Second,

        /// <summary>
        ///     分钟
        /// </summary>
        Minute,

        /// <summary>
        ///     刻
        /// </summary>
        Quarter,

        /// <summary>
        ///     小时
        /// </summary>
        Hour,

        /// <summary>
        ///     天
        /// </summary>
        Day,

        /// <summary>
        ///     星期
        /// </summary>
        Week,

        /// <summary>
        ///     月
        /// </summary>
        Month,

        /// <summary>
        ///     年
        /// </summary>
        Year
    }
}